package exercise.coroutine;

import exercise.coroutine.easy.q1.Order;
import exercise.coroutine.easy.q1.Result;
import exercise.coroutine.easy.q1.User;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 并行调用多个接口 / 数据库，汇总结果
 */
public class ConcurrencyJavaQ1 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        CompletableFuture<User> userFuture = CompletableFuture.supplyAsync(User::new, executorService);
        CompletableFuture<Order> orderFuture = CompletableFuture.supplyAsync(Order::new, executorService);
        CompletableFuture<Result> resultFuture = userFuture.thenCombine(orderFuture, Result::new);
        resultFuture.join(); // 阻塞等结果
    }
}
